package net.chrisrichardson.ec2deployer.applications

import net.chrisrichardson.ec2deployer.util.FileBasedTemplate
import groovy.mock.interceptor.MockFor

class ApacheConfTemplateTest extends GroovyTestCase {

    public void test() {
        FileBasedTemplate fileBasedTemplate = new FileBasedTemplate()
        def mockForTomcat = new MockFor(TomcatServer)
        mockForTomcat.demand.getHostPrivateDnsName { "baz.ip" }
        mockForTomcat.demand.getJvmRoute { "route.66" }
        mockForTomcat.use {
            TomcatServer tomcatServer = new TomcatServer()
            String result = fileBasedTemplate.process("/templates/cluster.conf", [contexts: ["foo", "bar"], tomcats: [tomcatServer]])
            assertEquals """Listen 127.0.0.1:8083

<VirtualHost *:8083>
    <Location /server-status>
        SetHandler server-status
    </Location>
</VirtualHost>

ExtendedStatus On

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass /foo balancer://mycluster/foo stickysession=JSESSIONID

ProxyPass /bar balancer://mycluster/bar stickysession=JSESSIONID

<Proxy balancer://mycluster>

BalancerMember ajp://baz.ip:8009 route=route.66 min=1 keepalive=On retry=5
</Proxy>
""", result
        }
    }

    public void testSslConf() {
        FileBasedTemplate fileBasedTemplate = new FileBasedTemplate()
        def mockForTomcat = new MockFor(TomcatServer)
        mockForTomcat.demand.getActualContexts { ["foo", "bar"] }
        mockForTomcat.demand.getHostPrivateDnsName { "baz.ip" }
        mockForTomcat.demand.getJvmRoute { "route.66" }
        mockForTomcat.use {
            TomcatServer tomcatServer = new TomcatServer()
            String result = fileBasedTemplate.process("/templates/ssl.conf", [tomcats: [tomcatServer]])
            assertEquals """Listen 443

LoadModule ssl_module modules/mod_ssl.so

SSLSessionCache         shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout  300

SSLMutex default

SSLRandomSeed startup file:/dev/urandom  256
SSLRandomSeed connect builtin

<VirtualHost *:443>

DocumentRoot "/var/www/html"

SSLEngine on

AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl    .crl

SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP

SSLCertificateFile /etc/httpd/conf/my.cert
SSLCertificateKeyFile /etc/httpd/conf/my.key

<Proxy *>
Order deny,allow
Allow from all
</Proxy>

ProxyPass /foo balancer://mysslcluster/foo stickysession=JSESSIONID

ProxyPass /bar balancer://mysslcluster/bar stickysession=JSESSIONID

<Proxy balancer://mysslcluster>

BalancerMember ajp://baz.ip:8010 route=route.66 min=1 keepalive=On retry=5
</Proxy>

</VirtualHost>

""", result
        }
    }

}